package com.union.core.sys.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.sys.dao.PublicSentAudiDao;
import com.union.core.sys.model.PublicSentAudi;
import com.union.core.sys.model.PublicSentimentModule;
import com.union.core.sys.service.SysService;
import com.union.core.sys.util.BaseDateConfig;

/**
 * 舆情评阅DAO实现类 filename: SysManagerDaoImpl.java project: CCTR
 * 
 * @company Hainan Jowe Technology Ltd
 * @author wull
 * @create in 2012-10-31
 * @version 1.0
 */
public class PublicSentAudiDaoImpl implements PublicSentAudiDao {

	private static Logger log = Logger.getLogger(PublicSentAudiDaoImpl.class
			.getName());

	private DBOperation opera;
	private PublicSentAudi publicSentAudi;
	private SysService sysService = SysService.getInstance();

	public PublicSentAudiDaoImpl() {
	}

	public boolean addAudi(int publicSentId,PublicSentAudi publicSentAudi) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			StringBuffer sqlBuf = new StringBuffer();
			sqlBuf.append("insert into public_sentiment_audi");
			sqlBuf.append("(pubsentm_id,audi_details,status,");
			sqlBuf.append(" pubsentm_file,audi_time,staff_id,organization_id,update_staff,update_time,organization_name,staff_name,file_path) ");
			sqlBuf.append("values(?,?,?,?,?,?,?,?,SYSDATE(),?,?,?);");
			opera.prepStatment(sqlBuf.toString());
			opera.setInt(1, publicSentId);	
			opera.setString(2, publicSentAudi.getAudi_details());
			opera.setInt(3, publicSentAudi.getStatus());
			opera.setString(4, publicSentAudi.getPubsentm_file());
			opera.setString(5, publicSentAudi.getAudi_time());
			opera.setInt(6, publicSentAudi.getStaff_id());
			opera.setInt(7, publicSentAudi.getOrganization_id());
			opera.setInt(8, sysService.getSessionListener().getPERSONNEL_ID());
			opera.setString(9, publicSentAudi.getOrganization_name());
			opera.setString(10, publicSentAudi.getStaff_name());
			opera.setString(11, publicSentAudi.getFile_path());
		
			opera.execute();
			
			result = opera.transcationCommit();
			if(result == false)
			{
				log.error("sql="+opera.getPres().toString());
				return false; 
			}
		} catch (Exception e) {
			log.error(e);
			log.error("sql="+opera.getPres().toString());
		} finally {
			opera.closePres();
			opera.closeConn();
			log.error("sql="+opera.getPres().toString());
		}
		
		return result;      
	}
	
	
	
	public boolean deleteAudi(int pubSentId) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			StringBuffer sqlBuf = new StringBuffer();
			sqlBuf.append("delete from public_sentiment_audi");
			sqlBuf.append(" where pubsentm_id = ? ");
			opera.prepStatment(sqlBuf.toString());
			opera.setInt(1, pubSentId);

			
			 opera.execute();
			
			result = opera.transcationCommit(); 
			if(result == false)
			{
				log.error("sql="+opera.getPres().toString());
				return false;
			}
			StringBuffer sqlBuf1 = new StringBuffer();
			sqlBuf1.append("insert into public_sentiment_audi_log");
			sqlBuf1.append("(audi_id,pubsentm_id,update_staff,update_time,status,oper_status)");
			sqlBuf1.append(" select audi_id,pubsentm_id,?,SYSDATE(),'0',?");
			sqlBuf1.append(" from public_sentiment_audi   ");
			sqlBuf1.append(" where pubsentm_id = ? "); 
			opera.prepStatment(sqlBuf1.toString()); 
			opera.setInt(1, sysService.getSessionListener().getPERSONNEL_ID());
			opera.setInt(2, BaseDateConfig.DELETE_DATA);
			opera.setInt(3, pubSentId);
			

			opera.execute();
		
			result = opera.transcationCommit();
			if (result == false) {
				log.error("sql="+opera.getPres().toString());
				return false;
			}
			
			
		} catch (Exception e) {
			log.error(e);
			log.error("sql="+opera.getPres().toString());
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return result;      
	}
	
	
	
	
	public List<PublicSentAudi> getPubSentAudiList(int publicSentId,int status)
	{
		int totalSize = 0;// 某条件查询下记录的总条数
		List<PublicSentAudi> pubSentAudiList = new ArrayList<PublicSentAudi>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			StringBuffer sql = new  StringBuffer();
			PublicSentimentModule publicSentimentModule;
				
			sql.append("select audi_id,pubsentm_id,audi_details,status,audi_time,");
			sql.append("staff_id,pubsentm_file,update_staff,update_time,organization_id,organization_name,staff_name,file_path ");
			sql.append(" from public_sentiment_audi");
			sql.append(" where pubsentm_id = ? ");
			sql.append(" and status = ? order by audi_time desc ");
			opera.prepStatment(sql.toString());
			opera.setInt(1, publicSentId);
			opera.setInt(2, status);
			ResultSet res = opera.executeQuery();
			int i =0; 
			while (res.next()) {
				PublicSentAudi  publicSentAudi= new PublicSentAudi();
				publicSentAudi.setAudi_id(res.getInt(1));
				publicSentAudi.setPubsentm_id(res.getInt(2));
				publicSentAudi.setAudi_details(res.getString(3));
				publicSentAudi.setStatus(res.getInt(4));
				publicSentAudi.setAudi_time(res.getString(5));
				publicSentAudi.setStaff_id(res.getInt(6));
				publicSentAudi.setPubsentm_file(res.getString(7));
				publicSentAudi.setUpdate_staff(res.getInt(8));
				publicSentAudi.setUpdate_time(res.getString(9));
				publicSentAudi.setOrganization_id(res.getInt(10));
				publicSentAudi.setOrganization_name(res.getString(11));
				publicSentAudi.setStaff_name(res.getString(12));
				publicSentAudi.setFile_path(res.getString(13));
				pubSentAudiList.add(i,publicSentAudi);
				i++; 
			}
			
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return pubSentAudiList;
		
	}

	public void setPublicSentAudi(PublicSentAudi publicSentAudi) {
		this.publicSentAudi = publicSentAudi;
	}

	public PublicSentAudi getPublicSentAudi() {
		return publicSentAudi;
	}
	

}
